package com.membership.cloud.jpa.repository;

import com.membership.cloud.jpa.domain.Customer;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;

/**
 * Created by hiephn on 2014/06/28.
 */
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    @Query("select c from Customer c where c.name like ?1 and " +
            "c.fullName like ?2 and " +
            "c.barcode like ?4 and c.phone like ?5 and " +
            "c.email like ?5 order by c.name")
    Page<Customer> getByStringAttributesOrderByNameAsc(
            String name, String fullName, String barcode,
            String phone, String email, Pageable pageable);
    @Query("select c from Customer c where c.name like ?1 and " +
            "c.fullName like ?2 and " +
            "c.barcode like ?4 and c.phone like ?5 and " +
            "c.email like ?6 and c.birthday = ?7 order by c.name")
    Page<Customer> getByStringAttributesAndBirthDayOrderByNameAsc(
            String name, String fullName, String barcode,
            String phone, String email, Timestamp birthday, Pageable pageable);

    @Query("select c from Customer c where c.name like ?1 or " +
            "c.fullName like ?1 or " +
            "c.barcode like ?1 or c.phone like ?1 or " +
            "c.email like ?1 order by c.name")
    Page<Customer> filterByStringAttributesOrderByNameAsc(
            String filterKey, Pageable pageable);
    @Query("select c from Customer c where c.name like ?1 or " +
            "c.fullName like ?1 or " +
            "c.barcode like ?1 or c.phone like ?1 or " +
            "c.email like ?1 or c.birthday = ?2 order by c.name")
    Page<Customer> filterByStringAttributesOrBirthDayOrderByNameAsc(
            String filterKey, Timestamp birthDay, Pageable pageable);

    int findByPhone(String phone);
    int findByEmail(String email);
}
